package fly.gdx.desktop.window;


import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import fly.gdx.graphics.FlyRenderer2D;
import fly.shared.Event;

public class FlyWindow extends ApplicationAdapter
{
	private LwjglApplication application;
	private LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
	
	private Event<Object> event_init = null;
	public Event<FlyRenderer2D> event_render = null;
	
	private FlyRenderer2D renderer;

	
	public FlyWindow() {this("",800,600,null);}
	public FlyWindow(String title) {this(title,800,600,null);}
	public FlyWindow(String title, int w, int h) {this(title,w,h,null);}
	public FlyWindow(String title, int w, int h, Event<Object> event_init)
	{
		this.event_init = event_init;
		
		config.title = title;
		config.width = w;
		config.height = h;
		application = new LwjglApplication(this, config);
	}
	
	
	@Override
	public void create ()
	{
		renderer = new FlyRenderer2D();
		
		if(this.event_init != null)
			this.event_init.invoke(null);
		this.event_init = null;

	}

	@Override
	public void render ()
	{
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		if(event_render != null)
		{
			renderer.Begin(ShapeRenderer.ShapeType.Line);
			event_render.invoke(renderer);
			renderer.End();
		}
	}
	
	@Override
	public void dispose()
	{
		event_render = null;
		renderer.Dispose();
	}
	
	//public void SetConfig(LwjglApplicationConfiguration config) {this.config = config;}
	public void SetTitle(String title) {this.application.getGraphics().setTitle(title);}
	//public void SetWidth(int w) {}
	//public void SetHeight(int h) {}
	
	public LwjglApplicationConfiguration GetConfig() {return this.config;}
	public String GetTitle() {return this.config.title;}
	public int GetWidth() {return this.application.getGraphics().getWidth();}
	public int GetHeight() {return this.application.getGraphics().getHeight();}
	
	public void SetEventRender(Event<FlyRenderer2D> render_event)
	{
		this.event_render = render_event;
	}
	
	
}




















